package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateSysTag
//@description: 创建SysTag记录
//@param: sysTag model.SysTag
//@return: err error

func CreateSysTag(sysTag model.SysTag) (err error) {
	err = global.GVA_DB.Create(&sysTag).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteSysTag
//@description: 删除SysTag记录
//@param: sysTag model.SysTag
//@return: err error

func DeleteSysTag(sysTag model.SysTag) (err error) {
	err = global.GVA_DB.Delete(&sysTag).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteSysTagByIds
//@description: 批量删除SysTag记录
//@param: ids request.IdsReq
//@return: err error

func DeleteSysTagByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.SysTag{}, "id in ?", ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateSysTag
//@description: 更新SysTag记录
//@param: sysTag *model.SysTag
//@return: err error

func UpdateSysTag(sysTag model.SysTag) (err error) {
	err = global.GVA_DB.Save(&sysTag).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetSysTag
//@description: 根据id或标签名称获取SysTag记录
//@param: id uint
//@return: err error, sysTag model.SysTag

func GetSysTag(id uint, tagName string) (err error, sysTag model.SysTag) {
	err = global.GVA_DB.Where("id = ?", id).Or("tag_name=?", tagName).First(&sysTag).Error
	return
}

// 查询所有已审核的标签
func GetSysTags() (err error,  sysTags []model.SysTag) {
	err = global.GVA_DB.Where("tag_status", "已审核").Find(&sysTags).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetSysTagInfoList
//@description: 分页获取SysTag记录
//@param: info request.SysTagSearch
//@return: err error, list interface{}, total int64

func GetSysTagInfoList(info request.SysTagSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&model.SysTag{})
	var sysTags []model.SysTag
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.TagName != "" {
		db = db.Where("`tag_name` LIKE ?", "%"+info.TagName+"%")
	}
	if info.TagStatus != "" {
		db = db.Where("`tag_status` = ?", info.TagStatus)
	}
	if info.TagAuthor != "" {
		db = db.Where("`tag_author` LIKE ?", "%"+info.TagAuthor+"%")
	}
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&sysTags).Error
	return err, sysTags, total
}
